Remove slug from custom post type in WordPress

Remove slug from custom post type in WordPress

If you are a WordPress developer and love to create custom post types to addon the new feature in your project then this article is very helpful for you.

When you create a new custom post type in WordPress then you can see that by default, its slug will show in the permalink URL structure. Adding slug to permalink displays the post type of the current post and creates the URLs longer which is hard to remember. Also, it creates an issue to rank articles in Google.

So now I will cover all steps to remove slug from custom post type to make URL shorter and easy to remember.

Custom Post Type in WordPress

Recently, I created a custom post type called “Online Services” for a company. This resulted in permalinks such as example.com/online_services/title-of-online-service

As you can see, having permalinks like example.com/online_services/title-of-online-service is not nearly as clean, also not easy to remember and search engine optimized.

But having permalinks like example.com/title-of-online-service is nearly as clean, easy to remember, and SEO friendly.

As SEO (Search Engine Optimization) experts always suggest making URL shorter highly rank in Google.

I hope now you are clear with the article’s focused point. Now let’s see the solution:

Are you want to get implementation help, or modify or extend the functionality of this script?

A Tutorialswebsite Expert can do it for you.

Remove slug from custom post type

Step-1: Create your custom post type if you are not already created it till now.

Step-2: Copy and Paste the following code to your theme functions.php file to filter the permalink for our custom post type so that all published posts don’t have the slug in their URLs:

The above code will work, but conflicts may happen if the slug for your custom post type is the same as a page or post’s slug.

So removing the slug isn’t enough. You’ll get a 404 page if the slug for your custom post type is the same as a page or post’s slug because WordPress only expects posts and pages to behave this way.

Step-3: Now you also need to add the following: to your theme functions.php file to teach it that our custom post type’s posts can also have URLs like pages or posts.

If the above code will not work with your theme then remove the above code and try to use the below code:

That’s it! Just replace  “online_services” with the slug of your custom post type in both code samples. After this Go to Settings > Permalinks and saving the permalink structure to end in /%postname%/ may also be necessary.

Wrapping Words!

Thanks for reading 🙏, I hope you found the Remove slug from custom post type in WordPress Without Plugin tutorial helpful for your project. Keep learning!. If you face any problem – I am here to solve your problems.

Are you want to get implementation help, or modify or extend the functionality of this script? Submit a paid service request

Related posts